import { service } from "./index";

import type {
  LoginData,
  resDataType,
  UserListType,
  AddUserType,
  GoodsCateItem,
  AddCateParamType,
  IUpdateGoodsParams,
  IgoodsObj,
} from "@/dataType/index";

//调用登录接口
export const goLoginApi = (data: LoginData) => {
  return service.request<resDataType>({
    url: "/login",
    method: "POST",
    data: data,
  });
};

//调用菜单接口
export const getMenuApi = () => {
  return service.request<resDataType>({
    url: "/menus",
    method: "GET",
  });
};

//获取用户列表接口
export const getUserListApi = (params: UserListType) => {
  return service.request<resDataType>({
    url: "/users",
    method: "GET",
    params,
  });
};

//添加用户列表接口
export const addUserListApi = (data: AddUserType) => {
  return service.request<resDataType>({
    url: "/users",
    method: "post",
    data,
  });
};

//改变用户状态接口
export const changeUserStatusApi = (uId: number, type: boolean) => {
  return service.request<resDataType>({
    url: `/users/${uId}/state/${type}`,
    method: "PUT",
  });
};

//编辑用户接口

interface t {
  email: string;
  mobile: string;
}

//编辑用户接口
export const editUserApi = (id: number, data: t) => {
  return service.request<resDataType>({
    url: `users/${id}`,
    method: "PUT", //get,post,put,delete
    data,
  });
};

//获取角色列表接口
export const getRoleListApi = () => {
  return service.request<resDataType>({
    url: "/roles",
    method: "GET",
  });
};

//获取所有权限列表接口
export const getRightListApi = (type = "list") => {
  return service.request<resDataType>({
    url: `rights/${type}`,
    method: "GET",
  });
};

//调用删除角色指定权限的接口
export const deleteRightApi = (roleId: number, rightId: number) => {
  return service.request<resDataType>({
    url: `roles/${roleId}/rights/${rightId}`,
    method: "DELETE",
  });
};

//角色授权接口
export const setRightToRoleApi = (roleId: number, rids: string) => {
  return service.request<resDataType>({
    url: `/roles/${roleId}/rights`,
    method: "POST",
    data: { rids },
  });
};

//角色授权接口
export const getGoodsCateListApi = (params: GoodsCateItem) => {
  return service.request<resDataType>({
    url: "/categories",
    method: "GET",
    params,
  });
};

//添加分类接口
export const addGoodsCateApi = (data: AddCateParamType) => {
  return service.request<resDataType>({
    url: "/categories",
    method: "POST",
    data,
  });
};

//获取商品分类参数列表接口
export const getGoodsParamsListApi = (cateId: number, sel: string = "many") => {
  return service.request<resDataType>({
    url: `/categories/${cateId}/attributes`,
    method: "GET",
    params: { sel },
  });
};

//获取商品分类参数列表接口
export const updateGoodsParamsApi = (
  cateId: number,
  attrId: number,
  data: IUpdateGoodsParams
) => {
  return service.request<resDataType>({
    url: `/categories/${cateId}/attributes/${attrId}`,
    method: "PUT",
    data,
  });
};

//获取商品列表接口
export const getGoodsListApi = (params: UserListType) => {
  return service.request<resDataType>({
    url: "/goods",
    method: "GET",
    params,
  });
};

//添加商品接口
export const addGoodsApi = (data: IgoodsObj) => {
  return service.request<resDataType>({
    url: "/goods",
    method: "POST",
    data,
  });
};
